/**
 * zmn配件模块-配件列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author DaiZhichao
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'laypage', 'layedit' , 'upload'], function () {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var upload = layui.upload;

    var data = { "code":0, "msg":"", "count":json.totalCount, "items":json.items };


    //回显图片信息
    applyImages(json.items.idcardImgSrc,"idcardImgSrcImg","idcardImgSrcValue");
    applyImages(json.items.idcardImgSrc2,"idcardImgSrc2Img","idcardImgSrc2Value");
    applyImages(json.items.idcardImgSrc3,"idcardImgSrc3Img","idcardImgSrc3Value");
    applyImages(json.items.idcardImgSrc4,"idcardImgSrc4Img","idcardImgSrc4Value");
    //渲染表格其他数据
    form.val("zmn-part-image-form", json.items);
    //初始化文件上传插件
    uploaderImage("idcardImgSrc", "idcardImgSrcImg", "idcardImgSrcText", "idcardImgSrcValue");
    uploaderImage("idcardImg2Src", "idcardImgSrc2Img", "idcardImgSrc2Text", "idcardImgSrc2Value");
    uploaderImage("idcardImg3Src", "idcardImgSrc3Img", "idcardImgSrc3Text", "idcardImgSrc3Value");
    uploaderImage("idcardImg4Src", "idcardImgSrc4Img", "idcardImgSrc4Text", "idcardImgSrc4Value");

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param showId
     * @param errorId
     */
    function uploaderImage(elemId, showId, errorId, saveId) {
        upload.render({
            elem: '#' + elemId
            , url: '/part/uploader.action'
            , done: function (res) {
                if (res.code > 0) {
                    return layer.msg('上传失败');
                } else {
                    $('#' + showId).attr('src', res.data.src);
                    $('#' + saveId).attr('value', res.data.src);
                    return layer.msg('上传成功');
                }
            }
            , error: function () {
                let demoText = $('#' + errorId);
                demoText.html('<span style="color: #FF5722;">上传失败</span><a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploaderImage(elemId, showId, errorId);
                });
            }
        });
    }
    function applyImages(imageSrc ,showId,saveId) {
        if(imageSrc != null || imageSrc != undefined){
            $('#' + showId).attr('src', imageSrc);
            $('#' + saveId).attr('value', imageSrc);
        }
    }
    function forrmaterrRadioData(data) {
        if(data != null &&　data　!= undefined){
            return data.toString();
        }
        return "";
    }


});